Skip to content

test(coverage): drive db/cache/nosql local providers (91.1/88.9/85.9%)#148

Closed
mastermanas805 wants to merge 1 commit into
masterfrom
coverage/api-providers-dbcachenosql-95
Closed

test(coverage): drive db/cache/nosql local providers (91.1/88.9/85.9%)#148
mastermanas805 wants to merge 1 commit into
masterfrom
coverage/api-providers-dbcachenosql-95

Conversation

@mastermanas805
Copy link
Copy Markdown
Member

Summary

Adds real-backend integration + white-box unit tests for the three local-backend providers in internal/providers/.

Package Before After
internal/providers/db 2.2% 91.1%
internal/providers/cache 64.8% 88.9%
internal/providers/nosql 70.3% 85.9%

What's covered: provision + deprovision happy paths, name/identifier derivation, idempotent re-provision, duplicate-resource conflicts, connection-failure branches, ACL allowlist + key-namespace fallback, the full Neon HTTP backend (via an injected RoundTripper), and the provider factory/delegation layer.

Tests skip cleanly when TEST_CUSTOMERS_URL / TEST_REDIS_URL / TEST_MONGO_URI are unset, so a bare go test ./... stays green. The privilege-failure branches (DROP DATABASE / DROP USER denied) use an optional TEST_CUSTOMERS_LIMITED_URL non-superuser DSN.

Did NOT reach the ≥95% target — honest status

The remaining uncovered statements in each file are defensive code that is not deterministically unit-testable against real backends without adding production source seams. None of the three hit 95%:

  • db (need 7 more, ~0 cleanly reachable): crypto/rand failures; pgx conn.Close(ctx) defer-error logs (verified: a cancelled ctx does not make Close error); the REVOKE CONNECT / GRANT PRIVILEGES / GRANT SCHEMA non-fatal logs (the role that just created the DB owns it, so these can't fail); the connect-to-just-created-DB branch (always succeeds).
  • cache (need 4 more, ~0 reachable): crypto/rand.Read failure; the 200k-key StorageBytes truncation ceiling (one MEMORY USAGE round-trip per key → multi-minute, times out CI); the mid-scan key-deletion skip (timing race, would be flaky).
  • nosql (need 6 more, ~0 reachable): mongo.Disconnect defer-error logs; crypto/rand.Read; dbStats / drop-database errors that don't fire under a root connection.

Reaching ≥95% would require either flaky timing-dependent tests, an impractically slow 200k-key write, or refactoring the production source to inject fault-on-demand stdlib seams (rand source, mongo client factory, lowered constants). Flagging for a decision rather than shipping flaky tests or unrelated source churn.

Test plan

  • go test ./internal/providers/{db,cache,nosql} -count=1 -p 1 green against local docker pg(16)/redis(7)/mongo(6) on 127.0.0.1
  • go test ... -short green (graceful skip) with no env vars
  • gofmt -l clean, go vet clean
  • CI gate on the three packages

🤖 Generated with Claude Code

Adds real-backend + white-box unit tests for the three local-backend
providers:

- db: 2.2% -> 91.1% (local CREATE DATABASE/USER lifecycle, name
  derivation, dup-provision, connect/storage/drop error branches, the
  full neon HTTP backend via an injected RoundTripper, and the provider
  factory/delegation)
- cache: 64.8% -> 88.9% (New defaults, legacy/ full ACL username
  derivation, nil-client + Upstash-stub + ACL->key-namespace fallback,
  SCAN error, vanished-key skip)
- nosql: 70.3% -> 85.9% (New defaults, dup-user createUser error,
  positive StorageBytes, dropUser non-fatal, connect-error branches,
  init-insert + dbStats fail-open)

Tests are CI-safe: they skip cleanly when TEST_CUSTOMERS_URL /
TEST_REDIS_URL / TEST_MONGO_URI (and the optional
TEST_CUSTOMERS_LIMITED_URL for privilege-failure branches) are unset.

The remaining uncovered statements in each file are defensive code that
is not deterministically unit-testable against real backends without
production source seams: crypto/rand failures, pgx Close /
mongo.Disconnect defer-error logs (no error on a cancelled ctx), the
200k-key StorageBytes truncation ceiling (O(keys) MEMORY USAGE
round-trips), and owner-only REVOKE/GRANT branches that cannot fail for
the role that just created the database. None reached the >=95% target;
see the PR body for the per-branch blocker analysis.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@mastermanas805
Copy link
Copy Markdown
Member Author

Superseded by #151, which reaches the >=95% mandate on all three packages (db 97.8% / cache 98.2% / nosql 100%) using deterministic source seams instead of waivers. #151 also fixes the env-var mismatch in this PR (TEST_CUSTOMERS_URL -> TEST_POSTGRES_CUSTOMERS_URL — these tests silently skipped in CI here) and replaces the timing-based MEMORY-USAGE-skip test with a deterministic interface fake.

mastermanas805 added a commit that referenced this pull request May 22, 2026
…rce seams (#151)

Closes the coverage gap on the three local-backend providers to the >=95%
mandate using deterministic source seams (org policy: seams, not waivers).
Production defaults are identical — every seam defaults to the real
fn/value.

Coverage (per-package, real pg16/redis7/mongo6 on 127.0.0.1):
- internal/providers/db:    2.2%  -> 97.8%  (local.go fully covered)
- internal/providers/cache: 64.8% -> 98.2%
- internal/providers/nosql: 70.3% -> 100.0%

Seams added (all package vars / interfaces, real default):
- db/local.go:    randInt (crypto/rand.Int), pgConn interface + pgxConnect
  factory — drives the RNG failure, conn.Close defer-error logs, and the
  non-fatal REVOKE/GRANT/DROP USER exec-error logs via an in-memory fake.
- cache/redis.go: randRead (crypto/rand.Read); storageBytesScanCap
  (test-lowerable truncation ceiling, default storageMaxKeys=200k);
  redisScanner interface behind storageBytes — drives the mid-scan
  vanished-key skip deterministically (fake returns a SCAN key that
  MEMORY USAGE reports missing) and the truncation branch with a handful
  of keys (no 200k write, no timing).
- nosql/mongo.go: randRead (crypto/rand.Read); mongoDisconnect seam for
  the Disconnect defer-error logs; storageSizeToInt64 extracted so every
  numeric BSON arm is unit-testable.

Replaces the flaky timing-based MEMORY-USAGE-skip test from the prior
attempt with a deterministic interface fake. Aligns the db test env var
to TEST_POSTGRES_CUSTOMERS_URL (what CI's coverage.yml provides; the prior
attempt used TEST_CUSTOMERS_URL and silently skipped in CI). Hardens
uniqueToken with a per-run random seed so same-second runs against a
shared Postgres can't collide.

Tests are CI-safe (skip cleanly with no env) and stable across 3
consecutive full runs. Supersedes PR #148.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant